package com.gopay.scheduler.job;

import com.gopay.common.constants.rcs.LoginMacLockStatus;
import com.gopay.common.domain.rcs.RcUserLoginMacDtl;
import com.gopay.common.rcs.dao.loginmacinfo.RcUserLoginMacDtlDao;
import com.gopay.scheduler.GopayBaseJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;

/**
 * CLASSNAME:AutoGenMacLoginClearJob
 * Description:次日清空当日临时锁定恶意登陆的mac地址信息
 * DATE:2017/7/10/010
 * AUTHER:PANSHICHAO
 * www.gopay.com.cn Inc.All rights reserved.
 */
public class AutoGenMacLoginClearJob extends GopayBaseJob{
    @Resource(name="rcUserLoginMacDtlDao")
    private RcUserLoginMacDtlDao rcUserLoginMacDtlDAO;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected void doJobInternal() {
        logger.error("次日清空当日临时锁定恶意登陆的mac地址信息任务开始");
        try{
            List<RcUserLoginMacDtl> lockNotForeverList = getMacLoginLockNotForeverInfo();
            if(lockNotForeverList !=null){
                logger.error("共有"+lockNotForeverList.size()+"条mac非永久锁定信息需要处理");
                for (RcUserLoginMacDtl dtl : lockNotForeverList){
                    logger.error("开始逐条处理，记录对应的Dtl_ID为:"+dtl.getDtlInfo()+",对应的锁定状态为:"+dtl.getLockType());
                    dtl.setLockType(LoginMacLockStatus.LOCK_STATUS_NERVER.value);
                    dtl.setErrorCount(0);
                    rcUserLoginMacDtlDAO.update(dtl);
                    logger.error(dtl.getDtlInfo()+"记录处理成功");
                }
            }else{
                logger.error("当前无需要处理的mac锁定信息");
            }
        }catch (Exception e){
            logger.error("次日清空当日临时锁定恶意登陆的mac地址信息任务出现异常,对应的异常信息为："+e.getMessage());
            logger.error("次日清空当日临时锁定恶意登陆的mac地址信息任务出现异常,对应的异常信息为："+e.getStackTrace());
        }
    }

    private List<RcUserLoginMacDtl> getMacLoginLockNotForeverInfo(){
        String sql ="from RcUserLoginMacDtl where lastUpdTs < trunc(:endLastUpdTs) and lastUpdTs >= trunc(:beginLastUpdTs)";
        Date nowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DATE,-1);
        Date beginLastUpdDate = calendar.getTime();
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("beginLastUpdTs",beginLastUpdDate);
        param.put("endLastUpdTs",nowDate);
        List<RcUserLoginMacDtl> resultList =rcUserLoginMacDtlDAO.findByHql(sql,param);
        List<RcUserLoginMacDtl> lockNotForeverList = new ArrayList<RcUserLoginMacDtl>();
        if(resultList != null && resultList.size()!=0){
            for(RcUserLoginMacDtl dtl : resultList){
                logger.error("信息的锁定状态为:"+dtl.getLockType());
                if(LoginMacLockStatus.LOCK_STATUS_FOREVER.value.equals(dtl.getLockType())|| (LoginMacLockStatus.LOCK_STATUS_NERVER.value.equals(dtl.getLockType())&& 0 == dtl.getErrorCount())){
                    continue;
                }
                lockNotForeverList.add(dtl);
            }
        }else{
            return null;
        }
        return lockNotForeverList;
    }
}
